package com.yy.wga.order.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yy.wga.common.core.dto.PageBean;
import com.yy.wga.common.core.exception.BusinessException;
import com.yy.wga.common.mysql.util.PageUtil;
import com.yy.wga.common.util.common.StringUtils;
import com.yy.wga.common.web.intereceptor.RequestHolder;
import com.yy.wga.order.feign.request.OrderGoodsPageRequest;
import com.yy.wga.order.feign.response.OrderGoodsDetailResponse;
import com.yy.wga.order.feign.response.OrderGoodsPageResponse;
import com.yy.wga.order.service.mapper.OrderGoodsMapper;
import com.yy.wga.order.service.pojo.OrderGoodsDO;
import com.yy.wga.order.service.service.OrderGoodsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * 商品购买订单信息(OrderGoods)表服务实现类
 *
 * @author guochao
 * @since 2024-10-26 10:34:35
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoodsDO> implements OrderGoodsService {

    @Override
    public PageBean<OrderGoodsPageResponse> pageData(OrderGoodsPageRequest request) {
        // 查询订单
        Long userId = request.getUserId();
        LambdaQueryWrapper<OrderGoodsDO> queryWrapper = new LambdaQueryWrapper<OrderGoodsDO>()
                .eq(OrderGoodsDO::getAppKey, RequestHolder.getBaseRequest().getAppKey())
                .eq(userId != null, OrderGoodsDO::getFromUserId, userId)
                .eq(request.getStatus() != null, OrderGoodsDO::getStatus, request.getStatus())
                .eq(StringUtils.isNotBlank(request.getOrderNo()), OrderGoodsDO::getOrderNo, request.getOrderNo())
                .gt(request.getStartPayTime() != null, OrderGoodsDO::getPayTime, request.getStartPayTime())
                .lt(request.getEndPayTime() != null, OrderGoodsDO::getPayTime, request.getEndPayTime())
                .orderByDesc(OrderGoodsDO::getId);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<OrderGoodsDO> orderGoodsList = baseMapper.selectList(queryWrapper);
        List<OrderGoodsPageResponse> responseList = com.yy.wga.common.utils.mapping.BeansUtils.mapList(orderGoodsList, OrderGoodsPageResponse.class);

        PageBean<OrderGoodsPageResponse> pageBean = PageUtil.setPageInfo(orderGoodsList);
        pageBean.setList(responseList);
        return pageBean;
    }

    @Override
    public OrderGoodsDetailResponse getDetail(Long id) {
        OrderGoodsDO orderGoods = baseMapper.selectOne(new LambdaQueryWrapper<OrderGoodsDO>()
                .eq(OrderGoodsDO::getAppKey, RequestHolder.getBaseRequest().getAppKey())
                .eq(OrderGoodsDO::getId, id));
        if (orderGoods == null) {
             throw new BusinessException("订单不存在");
        }
        return com.yy.wga.common.utils.mapping.BeansUtils.map(orderGoods, OrderGoodsDetailResponse.class);
    }

    @Override
    public List<OrderGoodsPageResponse> list(OrderGoodsPageRequest request) {
        LambdaQueryWrapper<OrderGoodsDO> queryWrapper = new LambdaQueryWrapper<OrderGoodsDO>()
                .eq(OrderGoodsDO::getAppKey, RequestHolder.getBaseRequest().getAppKey())
                .eq(OrderGoodsDO::getFromUserId, request.getFromUserId())
                .eq(request.getStatus() != null, OrderGoodsDO::getStatus, request.getStatus())
                .orderByDesc(OrderGoodsDO::getId);
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), false);
        List<OrderGoodsDO> orderGoodsList = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(orderGoodsList)) {
            return Collections.emptyList();
        }
        return com.yy.wga.common.utils.mapping.BeansUtils.mapList(orderGoodsList, OrderGoodsPageResponse.class);
    }
}
